﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using MongoDB.Bson.Serialization.Attributes;
using Ymatou.User.Dto;
using YmtAuth.Domain.Shard;
using YmtAuth.Common.Extend;
using System.Configuration;

namespace YmtAuth.Domain.Model.LoginAuth
{
    /// <summary>
    /// 用户洋码头登录票据。
    /// <remarks>value object</remarks>
    /// </summary>
    public class Bills
    {
        /// <summary>
        /// 洋码头ID
        /// </summary>
        public string LoginId { get; set; }
        /// <summary>
        /// 邮箱
        /// </summary>
        [BsonIgnoreIfNull]
        public string Email { get; set; }
        /// <summary>
        /// 手机
        /// </summary>
        [BsonIgnoreIfNull]
        public string Mobile { get; set; }

        public Bills(string loginId
                    , string email
                    , string mobile
                    , bool isVerify = false)
        {
            this.SetLoginId(loginId, false);
            //用户注册时邮箱，手机 二者选其一
            if (!string.IsNullOrEmpty(email))
                this.SetLoginEmail(email, isVerify);
            if (!string.IsNullOrEmpty(mobile))
                this.SetLoginMobile(mobile, isVerify);//去掉手机格式验证
        }

        public void SetLoginId(string loginId, bool isVerifyLoginId = true)
        {
            if (isVerifyLoginId)
            {
                var code = BillVerifySpecifications.VerifyLoginId(loginId);
                if (code != UserLoginBillsVerifyCode.Success)
                    throw new ArgumentException(code.ToString());
                this.LoginId = loginId.Trim().ToLower();
            }
            else
                this.LoginId = loginId.Trim().ToLower();
        }

        public void SetLoginEmail(string email, bool isVerify = true)
        {
            if (isVerify && CheckLoginKeyType(email) != LoginKeyType.Email)
                throw new ArgumentNullException("邮箱格式不正确");
            this.Email = email.Trim().ToLower();
        }

        public void SetLoginMobile(string mobile, bool isVerify = false)
        {
            if (isVerify && CheckLoginKeyType(mobile) != LoginKeyType.Mobile)
                throw new ArgumentNullException("手机格式不正确");
            this.Mobile = mobile.Trim();
        }

        public LoginKeyType CheckLoginKeyType(string loginKey)
        {
            //var rEmail = Regex.IsMatch(loginKey, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$", RegexOptions.IgnoreCase);
            if (loginKey.IsMail(ConfigurationManager.AppSettings["emailmatch"])) return LoginKeyType.Email;

            //var rMobile = Regex.IsMatch(loginKey, @"^[1]+[3,5,8]+\d{9}", RegexOptions.IgnoreCase);
            if (loginKey.IsMobile()) return LoginKeyType.Mobile;

            return LoginKeyType.LoginId;
        }

        protected Bills()
        {
        }
    }
}
